using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace PulseLibrary
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class InputManager : Microsoft.Xna.Framework.GameComponent
    {
        private static KeyboardState keyboardState;
        private static KeyboardState lastKeyboardState;
        private static Dictionary<String, Keys> keyBinds;

        public static KeyboardState KeyboardState
        {
            get { return keyboardState; }
        }

        public static KeyboardState LastKeyboardState
        {
            get { return lastKeyboardState; }
        }

        private static Dictionary<String, Keys> KeyBinds
        {
            get { return keyBinds; }
        }

        public InputManager(Game game)
            : base(game)
        {
            keyboardState = Keyboard.GetState();
            lastKeyboardState = keyboardState;
            keyBinds = new Dictionary<String, Keys>();
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public void AddKeyBinding(String name, Keys button)
        {
            Keys key = Keys.None;
            if (!keyBinds.TryGetValue(name, out key))
                keyBinds.Add(name, button);
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            lastKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();

            base.Update(gameTime);
        }

        /// <summary>
        /// Clear the "input" buffer so that the previous keyboard state doesn't intefere with the current keyboard state.
        /// Good for changing game states without inteference from previous states.
        /// </summary>
        public static void Flush()
        {
            lastKeyboardState = keyboardState;
        }

        /// <summary>
        /// Determine if a key has just been let go.
        /// Examples: Charging a shot, then letting go of the button to fire.
        /// </summary>
        /// <param name="key"></param>
        public static bool KeyReleased(Keys key)
        {
            return keyboardState.IsKeyUp(key) &&
            lastKeyboardState.IsKeyDown(key);
        }

        /// <summary>
        /// Determine if a key has been pressed for the first time.
        /// </summary>
        /// <param name="key"></param>
        public static bool KeyPressed(Keys key)
        {
            return keyboardState.IsKeyDown(key) &&
            lastKeyboardState.IsKeyUp(key);
        }

        /// <summary>
        /// Determine if the key has been/is held down for any duration of time.
        /// </summary>
        /// <param name="key"></param>
        public static bool KeyDown(Keys key)
        {
            return keyboardState.IsKeyDown(key);
        }

        public static bool KeyBindingReleased(String actionName)
        {
            return KeyReleased(keyBinds[actionName]);
        }

        public static bool KeyBindingDown(String actionName)
        {
            return KeyDown(keyBinds[actionName]);
        }

        public static bool KeyBindingPressed(String actionName)
        {
            return KeyPressed(keyBinds[actionName]);
        }
    }
}
